<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/7/21
 * Time: 17:03
 */
namespace app\franchisee\controller;
use think\Db;
use think\Request;
class My extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    //banner图
    public function banner(){
        extract(input());
        $res = Db::table('up_banner')
            ->where('banner_cate','in','1,4')
            ->where('banner_status',1)
            ->field('banner_id,banner_thumb,banner_type,banner_link')
            ->order('banner_sort desc')
            ->select();
        return json(['status'=>200,'msg'=>$res]);
    }
    //banner图详情
    public function bannerDetail(){
        extract(input());
        $id = input('param.id');
        $banner_content = Db::table('up_banner')->where('banner_id',$id)->value('banner_content');
        return json(['status'=>200,'msg'=>$banner_content]);
    }

    //个人资料
    public function userList(){
        extract(input());
        $res = Db::table('up_u_user')->where('user_id',$user_id)
            ->field('user_image,user_username,user_real_name,user_create_id,
            user_card,user_company_name,user_company_detail,user_jms_address as user_jms_addr,user_yyzz')
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    //编辑个人资料
    public function updateUser(){
        extract(input());
        $user_image = input('param.user_image');
        $user_username = input('param.user_username');
        $user_real_name = input('param.user_real_name');
        $user_card = input('param.user_card');
        $user_company_name = input('param.user_company_name');
        $user_company_detail = input('param.user_company_detail');
        $user_jms_addr = input('param.user_jms_addr');
        $user_yyzz = input('param.user_yyzz');
        $data['user_image'] =$user_image;
        $data['user_username'] =$user_username;
        $data['user_real_name'] =$user_real_name;
        $data['user_card'] =$user_card;
        $data['user_company_name'] =$user_company_name;
        $data['user_company_detail'] =$user_company_detail;
        $data['user_jms_address'] =$user_jms_addr;
        if($user_yyzz!=''){
            $result1 =$this->yyzz($user_yyzz) ;
            if($result1['error_code']=='0'){
                if(strlen($result1['result']['res'][0]['value'])!=18){
                    return json(['status' => 0, 'msg' =>"营业执照上传有误,请上传清晰图片"]);
                }
            }else{
                return json(['status' => 0, 'msg' =>"营业执照上传有误,请上传清晰图片"]);
            }

            $data['user_yyzz'] =$user_yyzz;
            $data['user_company_name'] = $result1['result']['res'][8]['value'];
            $data['user_company_detail'] = $result1['result']['res'][7]['value'];
            $data['user_jms_address'] = $result1['result']['res'][7]['value'];
        }
        Db::table('up_u_user')->where('user_id',$user_id)->update($data);
        return json(['status' => 200, 'msg' => "更新成功"]);
    }
    //营业执照
    public function yyzz($user_yyzz){
        header('Content-type:text/html;charset=utf-8');
        $appkey1 = "f3cc570bd987000b15338314e118314f";//营业执照
        $user_yyzz1 = master_imgUrl_base64($user_yyzz);//营业执照转base64
        $url1 = "http://apis.juhe.cn/businessLicenseOcr/index";
        $params1 = array(
            "image" => $user_yyzz1,//营业执照
            "key" => $appkey1,//应用APPKEY(应用详细页查询)
        );
        $paramstring1 = http_build_query($params1);
        $content1 = juhecurl($url1,$paramstring1,1);
        $result = json_decode($content1,true);
        return $result;
    }
    //我的钱包
    public function balance(){
        extract(input());
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        return json(['status' => 200, 'msg' => $user_balance]);
    }

    //钱包明细
    public function balanceDetail(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $res=Db::table('up_user_balance')
            ->where('user_id',$user_id)
            ->where('balance_status',2)
            ->order('balance_time desc')
            ->limit($limit,$num)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //开票申请
    public function invoice(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $res=Db::table('up_invoice')
            ->where('user_id',$user_id)
            ->order('invoice_time desc')
            ->limit($limit,$num)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //详情
    public function invoiceDetail(){
        extract(input());
        $invoice_id = input('param.invoice_id');
        $res=Db::table('up_invoice')
            ->where('invoice_id',$invoice_id)
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    //修改开票信息
    public function updateInvoice(){
        extract(input());
        $where['invoice_id'] = input('param.invoice_id');
        $data['invoice_company'] = input('param.invoice_company');
        $data['invoice_address'] = input('param.invoice_address');
        $data['invoice_phone'] = input('param.invoice_phone');
        $data['invoice_bank'] = input('param.invoice_bank');
        $data['invoice_account'] = input('param.invoice_account');
        $data['invoice_num'] = input('param.invoice_num');
        $res =  Db::table('up_invoice')->where($where)->update($data);
        if($res){
            return json(['status' => 200, 'msg' => "成功"]);
        }else{
            return json(['status' => 200, 'msg' => "失败"]);
        }
    }
    //申请开票
    public function sqInvoice(){
        extract(input());
        $where['invoice_id'] = input('param.invoice_id');
        $data['invoice_time'] = date('Y-m-d H:i:s');
        $data['invoice_status'] = 2;
        Db::table('up_invoice')->where($where)->setField($data);
        return json(['status' => 200, 'msg' => "成功"]);
    }
    //确认收到开票申请
    public function qrInvoice(){
        extract(input());
        $where['invoice_id'] = input('param.invoice_id');
        Db::table('up_invoice')->where($where)->setField('invoice_status',4);
        return json(['status' => 200, 'msg' => "成功"]);
    }

    //加盟商和企业的关系
    public function jmsQy(){
        extract(input());
        $res = Db::table('up_invoice_union')
            ->where('union_jms_user_id',$user_id)->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    public function jmsGx(){
        extract(input());
        $union_id = input('param.union_id');
        $where['union_id'] = $union_id;
        $res = Db::table('up_invoice_union')->where($where)->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    public function updateJms(){
        extract(input());
        $where['union_id'] = input('param.union_id');
        $data['union_company'] = input('param.union_company');
        $data['union_address'] = input('param.union_address');
        $data['union_phone'] = input('param.union_phone');
        $data['union_bank'] = input('param.union_bank');
        $data['union_account'] = input('param.union_account');
        $data['union_num'] = input('param.union_num');
        $res =  Db::table('up_invoice_union')->where($where)->update($data);
        if($res){
            return json(['status' => 200, 'msg' => "成功"]);
        }else{
            return json(['status' => 200, 'msg' => "失败"]);
        }
    }

    public function getCityAndHot()
    {
        $citys = Db::table('up_area')->where('level',2)->select();
        $data = array();
        foreach ($citys as $k=>$v){
            $str = $v['district'];
            $str= iconv("UTF-8","gb2312", $str);
            $tmp=bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
                if($v['district'] == '衢州市'){$letter = 'Q';}
                if($v['district'] == '亳州市'){$letter = 'B';}
                if($v['district'] == '濮阳市'){$letter = 'P';}
                if($v['district'] == '漯河市'){$letter = 'L';}
                if($v['district'] == '儋州市'){$letter = 'D';}
                if($v['district'] == '泸州市'){$letter = 'L';}
                $data[$k]['id'] = $v['district_id'];
                $data[$k]['name'] = $v['district'];
                $data[$k]['letter'] = $letter;

            }
        }
        $last_names = array_column($data,'letter');
        array_multisort($last_names,SORT_ASC,$data);
        $start = '';
        $res = array();
        foreach ($data as $k=>$v){
            if($v['letter'] != $start){
                $start = $v['letter'];
            }
            $res[$start][] = $v;
        }
        //--------------------
        $hot = Db::table('up_area')
            ->where('level',2)
            ->select();
        return json(['status'=>200,'msg'=>$res,'hot'=>$hot]);
//    dump($data);
//    dump($res);

    }
    public function getLetter($num){
        $limit = array( //gb2312 拼音排序
            array(45217,45252), //A
            array(45253,45760), //B
            array(45761,46317), //C
            array(46318,46825), //D
            array(46826,47009), //E
            array(47010,47296), //F
            array(47297,47613), //G
            array(47614,48118), //H
            array(0,0),     //I
            array(48119,49061), //J
            array(49062,49323), //K
            array(49324,49895), //L
            array(49896,50370), //M
            array(50371,50613), //N
            array(50614,50621), //O
            array(50622,50905), //P
            array(50906,51386), //Q
            array(51387,51445), //R
            array(51446,52217), //S
            array(52218,52697), //T
            array(0,0),     //U
            array(0,0),     //V
            array(52698,52979), //W
            array(52980,53688), //X
            array(53689,54480), //Y
            array(54481,55289), //Z
        );
        $char_index=65;
        foreach($limit as $k=>$v){
            if($num>=$v[0] && $num<=$v[1]){
                $char_index+=$k;
                return $char_index;
            }
        }
        return -1;
    }
    //设置支付密码
    public function setPassWord(){
        extract(input());
        $newpass = input('param.newpass');
        $oldpass = input('param.oldpass');
        $code = input('param.code');
        $phone  = input('param.phone');
        //=========验证验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码超时']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码失效']);
        if($newpass!=$oldpass){
            return json(['status' => 0, 'msg' => '2次密码不一致']);
        }else{
            $data['user_zf_password']= $newpass;
            Db::table('up_u_user')->where('user_id',$user_id)->update($data);
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            return json(['status'=>200,'msg'=>'成功']);
        }
    }

}